package com.xusheng.cli.controller.proxy;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.entity.rechargeModule.Recharge;
import com.xusheng.cli.req.RechargeQueryReq;
import com.xusheng.cli.service.FrontUserService;
import com.xusheng.cli.service.RechargeService;
import com.xusheng.cli.tools.CurrentProxyUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 充值表(Recharge)表控制层
 *
 * @author 王鹤然
 * @since 2023-07-03 12:38:12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/proxy/v1/recharge")
public class RechargeController {
    /**
     * 服务对象
     */
    @Resource
    private RechargeService rechargeService;
    @Resource
    private FrontUserService frontUserService;

    /**
     * 查询所有数据
     *
     * @param recharge 查询实体
     * @return 所有数据
     * @author 王鹤然
     * @since 2023-07-03 12:38:12
     */
    @GetMapping("queryAll")
    public Result<List<Recharge>> queryAll(Recharge recharge) {
        return Result.success(rechargeService.list(new QueryWrapper<>(recharge)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page     分页对象
     * @param recharge 查询实体
     * @return 所有数据
     * @author 王鹤然
     * @since 2023-07-03 12:38:12
     */
    @GetMapping("queryByPage")
    public Result<MyPage<Recharge>> queryByPage(MyPage<Recharge> page, RechargeQueryReq req) {
        List<FrontUser> list = frontUserService.lambdaQuery().eq(FrontUser::getProxyId, CurrentProxyUserUtil.getUserId()).list();
        List<Long> ids = list.stream().map(frontUser -> frontUser.getId()).collect(Collectors.toList());
        if (ids.size() > 0) {
            LambdaQueryWrapper<Recharge> eq = new LambdaQueryWrapper<>(new Recharge())
                    .ge(req.getStartTime() != null, Recharge::getCreateTime, req.getStartTime())
                    .ge(req.getEndTime() != null, Recharge::getCreateTime, req.getEndTime())
                    .in(Recharge::getPlayerId, ids);
            MyPage<Recharge> myPage = rechargeService.page(page, eq);
            for (Recharge recharge : myPage.getRecords()) {
                recharge.setPlayerAccount(frontUserService.getById(recharge.getPlayerId()).getAccount());
            }
            return Result.success(myPage);
        } else {
            return Result.success();
        }

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author 王鹤然
     * @since 2023-07-03 12:38:12
     */
    @GetMapping("queryOne/{id}")
    public Result<Recharge> selectOne(@PathVariable Serializable id) {
        return Result.success(rechargeService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param recharge 实体对象
     * @return 新增结果
     * @author 王鹤然
     * @since 2023-07-03 12:38:12
     */
    @PostMapping("insert")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Recharge recharge) {
        return Result.isSuccess(rechargeService.save(recharge));
    }

    /**
     * 修改数据
     *
     * @param recharge 实体对象
     * @return 修改结果
     * @author 王鹤然
     * @since 2023-07-03 12:38:12
     */
    @PutMapping("update")
    public Result<Object> update(@Validated(Update.class) @RequestBody Recharge recharge) {
        return Result.isSuccess(rechargeService.updateById(recharge));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author 王鹤然
     * @since 2023-07-03 12:38:12
     */
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        Recharge recharge = new Recharge();
        recharge.setId(id);
        return Result.isSuccess(rechargeService.removeById(recharge));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author 王鹤然
     * @since 2023-07-03 12:38:12
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(rechargeService.batchDelete(idList, new Recharge()));
    }
}

